package routing;

import com.aruba.utils.RBConnectionUtil;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import org.junit.Test;

public class Publisher {

    public static final String QUEUE_NAME1 = "q1";
    public static final String QUEUE_NAME2 = "q2";
    public static final String EXCHANGE_NAME = "routing";

    @Test
    public void publisher() throws Exception {
        //1. 获取连接对象
        Connection connection = RBConnectionUtil.getConnection();

        //2. 创建信道
        Channel channel = connection.createChannel();

        //3. 构建交换机
        channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.DIRECT);

        //4. 构建队列  参数：队列名 是否持久化 是否排外（只允许一个消费者） 长时间未使用是否自动删除 其他参数
        channel.queueDeclare(QUEUE_NAME1, false, false, false, null);
        channel.queueDeclare(QUEUE_NAME2, false, false, false, null);

        //5. 交换机与队列绑定 参数： 队列名 交换机名 Routing-Key
        channel.queueBind(QUEUE_NAME1, EXCHANGE_NAME, "a");
        channel.queueBind(QUEUE_NAME2, EXCHANGE_NAME, "b");
        channel.queueBind(QUEUE_NAME2, EXCHANGE_NAME, "c");

        //6. 发送消息
        //参数： 交换机 routing-Key 消息其他参数  消息
        channel.basicPublish(EXCHANGE_NAME, "a", null, "hi a".getBytes());
        channel.basicPublish(EXCHANGE_NAME, "b", null, "hi b".getBytes());
        channel.basicPublish(EXCHANGE_NAME, "c", null, "hi c".getBytes());
    }

}
